//
//  OrderTotalVC.m
//  sensen_ios
//
//  Created by mc on 16/2/25.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "OrderTotalVC.h"

#import "OrderMoreVC.h"
#import "PersonalVC.h"
#import "ShopVC.h"
#import "HMSegmentedControl.h"


@interface OrderTotalVC (){
    NSArray * aryTitle;  //订单标题
    NSArray * aryStatus; //订单状态
}


@property (strong, nonatomic)  HMSegmentedControl *segTitle;
@property (strong, nonatomic)  UIScrollView *svContent;
//@property (nonatomic, strong) UIImageView *remindView;

@end

@implementation OrderTotalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"我的订单";
    [self _initViews];
    [self _initData];
    
    // 创建导航栏左边返回按钮
    [self createLeftBarButtonItemWithImagename:nil];
    //[self createRightBarButtonItemsWithImagenameAry:@[@"下拉菜单按钮"]];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    // 设置导航栏颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
    // 隐藏标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main hideTabBar];

    self.view.backgroundColor = [UIColor whiteColor];
}


//#pragma mark - 创建视图
//// 右侧按钮的样式可以自定义，由imgname控制，如果是文字则调用下面的接口
//- (void)createRightBarButtonItemsWithImagenameAry:(NSArray *)imgnameAry {
//    NSMutableArray *itemsAry = [[NSMutableArray alloc] init];
//    for (int i = 0; i < imgnameAry.count; i ++) {
//        
//        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//        customView.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightClick:)];
//        [customView addGestureRecognizer:tap];
//        
//        // 图标
//        UIImageView *rightButton = [[UIImageView alloc] initWithFrame:CGRectMake((customView.width - 6)/2.0, 3, 6, 24)];
//        rightButton.userInteractionEnabled = YES;
//        [rightButton setImage:[UIImage imageNamed:imgnameAry[i]]];
//        [customView addSubview:rightButton];
//        
//        // 有消息时候的蓝点
//        _remindView = [[UIImageView alloc] initWithFrame:CGRectMake(rightButton.right + 3, rightButton.top - 2, 8, 8)];
//        _remindView.userInteractionEnabled = YES;
//        _remindView.image = [UIImage imageNamed:@"下拉菜单消息提示"];
//        [customView addSubview:_remindView];
//        
//        UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:customView];
//        [itemsAry addObject:rightBarButton];
//    }
//    self.navigationItem.rightBarButtonItems = itemsAry;
//}
//

#pragma mark -- 创建视图 --
- (void)_initViews{
    self.segTitle = [[HMSegmentedControl alloc] init];
    [self.view addSubview:self.segTitle];
    [self.segTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.height.equalTo(@(36));
    }];
    self.svContent = [[UIScrollView alloc] init];
    [self.view addSubview:self.svContent];
    [self.svContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.segTitle.mas_bottom).offset(0);
        make.bottom.equalTo(self.view).offset(0);
        
    }];

}

#pragma mark -- 初始化数据 --
- (void)_initData{
    aryTitle = @[@"全部",@"待付款",@"待发货",@"待收货"];//< 0:全部，1:待付款，2:待发货 3:待收货
    aryStatus = @[@0,@1,@2,@3];
    
    self.segTitle.sectionTitles = aryTitle;
    self.segTitle.backgroundColor = [UIColor whiteColor];
    self.segTitle.font = [UIFont boldSystemFontOfSize:14.0];
    self.segTitle.scrollEnabled = YES;
    self.segTitle.textColor = [BoyeTool colorFromHexRGB:@"999999"];
    self.segTitle.selectedTextColor = [BoyeTool colorFromHexRGB:@"ff5657"];
    self.segTitle.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segTitle.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segTitle.selectionIndicatorHeight = 2;
    self.segTitle.segmentEdgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.segTitle.backgroundColor = rgba(247, 247, 247, 1);
    __weak typeof(self) weakSelf = self;
    [self.segTitle setIndexChangeBlock:^(NSInteger index) {
        [weakSelf changeSelect:index animated:YES];
    }];
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 1)];
    lbl.backgroundColor = [BoyeTool colorFromHexRGB:@"dbdbdb"];
    
    [self.view addSubview:self.segTitle];
    [self.segTitle addSubview:lbl];
    
    self.svContent.pagingEnabled = YES;
    self.svContent.showsHorizontalScrollIndicator = NO;
    self.svContent.contentSize = CGSizeMake(SCREEN_WIDTH*aryTitle.count, 200);
    self.svContent.delegate = self;
    [self performSelector:@selector(delaySelect) withObject:nil afterDelay:0.1];
}

- (void)delaySelect{
    [self changeSelect:self.intFirstSelect animated:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    [self.segTitle setSelectedSegmentIndex:page animated:YES];
    [self addViewController:page];
}

- (void)changeSelect:(NSInteger)intSelect animated:(BOOL)animated{
    [self.segTitle setSelectedSegmentIndex:intSelect animated:animated];
    [self.svContent setContentOffset:CGPointMake(SCREEN_WIDTH*intSelect, 0) animated:animated];
    [self addViewController:intSelect];
}

- (void)addViewController:(NSInteger)index{
    NSMutableArray *maryIndex = [[NSMutableArray alloc]init];
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:[OrderMoreVC class]]) {
            //每次进入都重新下载View
            //[maryIndex addObject:[NSNumber numberWithInteger:vc.view.tag]];
        }
    }
    
    if (![maryIndex containsObject:[NSNumber numberWithInteger:index]]) {
        OrderMoreVC *vc =  [[OrderMoreVC  alloc] init];
        vc.order_state = [NSNumber numberWithInteger:index];
        [self addChildViewController:vc];
        vc.view.frame = CGRectMake(SCREEN_WIDTH*index, 0, self.svContent.frame.size.width, self.svContent.frame.size.height);
        [self.svContent addSubview:vc.view];
        vc.view.tag = index;
    }
}


#pragma mark- 点击事件
- (void)leftAction:(UIButton *)button {
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[PersonalVC class]]) {
            [self.navigationController popToViewController:vc animated:YES];
            break;
            return;
        }
    }
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[ShopVC class]]) {
            [self.navigationController popToViewController:vc animated:YES];
            break;
            return;
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
- (void)rightClick:(UITapGestureRecognizer *)tap{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
